package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.BottomSheetDialog;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.CoinTypeBean;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/05/04
 */
public class TransferActivity extends BaseActivity<TransferPresenter> {


    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.select_coin)
    HeySpinner selectCoin;
    @BindView(R2.id.account_edt)
    EditText accountEdt;
    @BindView(R2.id.num_edt)
    EditText numEdt;
    @BindView(R2.id.password_edt)
    EditText passwordEdt;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    List<CoinTypeBean> data;
    private Disposable typeDispo;
    private String pid;
    private BottomSheetDialog bottomSheetDialogPwd;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_transfer;
    }

    @Override
    public TransferPresenter getPresenter() {
        return new TransferPresenter();
    }

    @Override
    public void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_transferActivity1_j));
        selectCoin.setItemGravity(Gravity.CENTER);
        rightTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.mine_icon_history, 0);
        rightTv.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        mPresenter.getAssetType();

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(rightTv, () -> TransferRecordsActivity.start(this));
        ClickUtil.click(submitBtn, () -> {
            if (isEmpty(accountEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_transferActivity2_j));
                return;
            }

            if (isEmpty(numEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_transferActivity3_j));
                return;
            }

            if (isEmpty(passwordEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_transferActivity4_j));
                return;
            }

            mPresenter.transfer(numEdt.getText().toString(), pid,
                    passwordEdt.getText().toString(),
                    accountEdt.getText().toString()
            );


        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, TransferActivity.class);
        context.startActivity(intent);
    }


    public void updateAssetType(List<CoinTypeBean> data) {
        if (data == null || data.size() <= 0)
            return;
        this.data = data;
        pid = data.get(0).getPid();
        tvType.setText(data.get(0).getPname());
        typeDispo = Flowable.fromIterable(data)
                .map(coinTypeBean -> coinTypeBean.getPname())
                .toList()
                .subscribe((strings, throwable) -> {
                    selectCoin.attachData(strings);
                    selectCoin.setEnabled(true);
                    selectCoin.setOnSelectListener(position -> {
                        pid = data.get(position).getPid();
                        tvType.setText(data.get(position).getPname());
                    });
                });
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(typeDispo);
        super.onDestroy();
    }

    public void transferSuccess() {
        accountEdt.setText("");
        numEdt.setText("");
        passwordEdt.setText("");
        TransferRecordsActivity.start(this);
    }
}
